/**
*	Class: Container
*
* 	This class is used to represent containers of post-its all over the widget code.
*	Beyond it's attributes, it also has usefull auxiliar methods to handle it.
*
*	@param name - identifies the type of post-it that is stored
*	@param sortCriteria - the criteria of sorting post-it inside a container
*/
function Container(name, sortCriteria){
	
	this._name = name;
	if(typeof sortCriteria != "undefined"){
		this._sortCriteria = sortCriteria;
	}else{
		this._sortCriteria = COMPARE_BY_STARS; // Compare by stars is defined in PostIt.js
	}
	this._posts = new Array(0);

};
// Defining getters
Container.prototype.getName = function() {
			return this._name;
};
		
Container.prototype.getPosts = function() {
	return this._posts;
};

// Defining setters
Container.prototype.setName = function(name){
	if(typeof name == typeof "") // TODO fix this nasty piece of code
	this._name = name;
};

Container.prototype.setSortCriteria = function(sortCriteria) {
	this._sortCriteria = sortCriteria;
};


/**
*	Extract container into a XML format to communicate with the app. server.
*
*	@return string - the container in xml document format
*/
Container.prototype.genXml = function () { 	

	var xml = new String("");
	xml += "<" + this._name + ">"; 
	for(var i in this._posts){
		xml += this._posts[i].genXml();
	}
	xml += "</" + this._name + ">";
	return xml;
        };

// Defining Containers operations

/**
*	Method that adds a new post-it in given container
*
*/
Container.prototype.addPost = function(post){
	if(this.getPostIndex(post) >= this._posts.length)
		this._posts.push(post);
};
/**
*	Method that adds collection of post-its in given container
*
*/
Container.prototype.addPosts = function(posts){
	var index;
	for(index in posts){
		this.addPost(posts[index]);
	}
};

/**
*	Combine containers  
*/
Container.prototype.mergeContainer = function(container){ //TODO Check this
	this.addPosts(container._posts);
};

/**
*	Check if post-it, given as parameter, belongs to certain container
*	
*	@param post-it - key to check the post-it
*	@return bool - if the post-it belongs to the container returns true, else false
*/
Container.prototype.hasPostIt = function(post){
	var index = this.getPostIndex(post);
	if(index < this._posts.length){
		return true;
	}else{
		return false;
	}			
};

/**
*	Private method that verifies the position of post-it given as parameter
*
*	@private
*	@param post-it - key to look for position
*	@return int - index of post-it inside container
*/
Container.prototype.getPostIndex = function(post){
	var postId;
	if(typeof post == typeof 0){ // TODO change this to "static" string
		postId = post;
	}else{
		postId = post.getId();
	}
	var index = 0;
	var found = false;
	while(index < this._posts.length && found == false){
		if(this._posts[index].getId() == post.getId()){
			found = true;
		}else{
			index = index + 1;
		}
	
	}	
	return index;
};


/**
*	Remove post-it given as parameter from container
*	
*	@param post-it - post-it to be removed
*/
Container.prototype.removePost = function(post){
	var index = this.getPostIndex(post); // Check the PostIt existance
	if(index < this._posts.length){
		this._posts[index] = this._posts[this._posts.length - 1];
		this._posts.pop();	// Ignore the poped item, now that it has overrided the removed item
	}
};

/**
*	@return int - amount of post-its inside container
*/
Container.prototype.getPostsAmmount = function(){
	return this._posts.length;	
};	

/**
*	Sort post-its inside container by criteria given as parameter
*
*	@param sortCriteria - the criteria of sorting post-it inside a container
*/
Container.prototype.sortPosts = function(sortCriteria){
	if(typeof sortCriteria != "undefined"){
		this._posts.sort(sortCriteria);
	}else{
		this._posts.sort(this._sortCriteria);
	}
};

